package com.mlan.userap;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.mlan.userap.activity.BaseActivity;
import com.mlan.userap.component.FaceManager;
import com.mlan.userap.model.MsgEvent;
import com.mlan.userap.model.MsgResp;
import com.mlan.userap.model.OpCode;
import com.mlan.userap.okhttp.JsonCallBackData;
import com.mlan.userap.okhttp.RequestBuilder;
import com.mlan.userap.okhttp.model.LoginResponse;
import com.mlan.userap.untils.ConfigManager;
import com.mlan.userap.untils.Convert;
import com.mlan.userap.untils.LogHelper;
import com.mlan.userap.untils.PermissionHelper;
import com.mlan.userap.untils.SPUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SplashActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        AppConfig.token = SPUtils.getString("token", "");
        AppConfig.userId = SPUtils.getLong("userId", 0L);
        XXPermissions.with(this)
                // 申请单个权限
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
                //.unchecked()
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                        if (allGranted) {
                            Start();
                        }
                    }

                    @Override
                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(SplashActivity.this, permissions);
                        }
                    }
                });


//        XXPermissions xxPermissions = XXPermissions.with(this);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU){
//            xxPermissions
//                    .permission(Permission.READ_MEDIA_IMAGES)
//                    .permission(Permission.READ_MEDIA_AUDIO)
//                    .permission(Permission.READ_MEDIA_VIDEO);
//        }else {
//            xxPermissions
//                    // 申请多个权限
//                    .permission(Permission.READ_EXTERNAL_STORAGE)
//                    .permission(Permission.WRITE_EXTERNAL_STORAGE);
//        }
//        // 申请单个权限
//        xxPermissions.request(new OnPermissionCallback() {
//
//                    @Override
//                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
//                        if (!allGranted) {
//                            return;
//                        }
//                        Start();
//                    }
//
//                    @Override
//                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
//                        if (doNotAskAgain) {
//                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(SplashActivity.this, permissions);
//                        }
//                    }
//                });
    }

    private void Start() {
        if (FaceManager.getInstance().loadEnd) {
            ToNext();
        } else {
            FaceManager.loadEmojis();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void msgEvent(MsgEvent event) {
        if (event.code.equals(OpCode.LoadEmojiEnd.toString())) {
            ToNext();
        }
    }

    public void ToMain() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

    public void ToNext() {
        if (!AppConfig.token.isEmpty()) {
            new RequestBuilder("/User/LoginWithToken")
                    .getData(new JsonCallBackData<LoginResponse>() {
                        @Override
                        public void onSuccess(LoginResponse result) {
                            ConfigManager.saveString("user", result.getToken());
                            AppConfig.userId = result.getUserId();
                            ToMain();
                        }

                        @Override
                        public void onError(Exception error) {
                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                            finish();
                        }
                    });
        } else {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        }
    }


}
